package org.umlviewer.xmiexporter;

import org.jdom2.Element;
import org.jdom2.Namespace;
import org.umlviewer.model.Constants;
import org.umlviewer.model.Relationship;

/**
 *
 * @author Rafa
 */
public class AssociationExporter extends RelationExporter {

    public AssociationExporter(Relationship r, int type){
        super(r, type);
    }
    
    public Element xmiSerialize(){
        Namespace xmins = Constants.XMINAMESPACE;
        Element asos = new Element("ownedMember");
        boolean needSrc = false, needDst = false;
        asos.setAttribute("type","uml:Association",xmins);
        asos.setAttribute("id",getId(),xmins);
        asos.setAttribute("name",getRel().getLabel());
        asos.setAttribute("visibility","public");        
        if(getTipo() == 1){
            needSrc = true;
            needDst = true;
        }
        else if(getTipo() == 2){
            needSrc = false;
            needDst = true;
        }
        
        if(needDst){
        //el hijo que guarda la referencia al destino
            Element dst = new Element("ownedEnd");
            dst.setAttribute("type","uml:Property",xmins);
            dst.setAttribute("id","dst"+getId(),xmins); //se utiliza el mismo id del padre - por ahora
            dst.setAttribute("visibility","public");
            if(getRel().getType() == Relationship.AGGREGATION)
                dst.setAttribute("aggregation","shared");
            else if(getRel().getType() == Relationship.COMPOSITION)
                dst.setAttribute("aggregation","composite");
            dst.addContent(new Element("type").setAttribute("idref",getRel().getDestination().getId(),xmins));
            asos.addContent(dst);
        }
        if(needSrc){
            //el hijo con la referencia al origen
            Element dst = new Element("ownedEnd");
            dst.setAttribute("type","uml:Property",xmins);
            dst.setAttribute("id","src"+getId(),xmins); //se utiliza el mismo id del padre - por ahora
            dst.setAttribute("visibility","public");
            if(getRel().getType() == Relationship.AGGREGATION)
                dst.setAttribute("aggregation","shared");
            else if(getRel().getType() == Relationship.COMPOSITION)
                dst.setAttribute("aggregation","composite");
            dst.addContent(new Element("type").setAttribute("idref",getRel().getSource().getId(),xmins));
            asos.addContent(dst);
        }
        return asos;
    }
    
}
